<?php
/*
	
*/
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();

if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['name'])){
		$params['name']=trim($_RQ['name']);
		$return['params']['name']=$params['name'];
	}
	$fields=array('id','name','type','isrecommend','ispermission','createtime');
	$parameter=array('params'=>$params,'limit'=>array($pindex,$psize),'fields'=>$fields,'orderby'=>'id desc');
	$list=$this->get_list('game',$parameter,$total);
	if($list){
		foreach($list as &$item){
		$item['url']=custom_url(array('jy_h5game/welcome/',array('id'=>$item['id'])),true);			
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['htmls']=array (
		  'type' => 'list',
		  'tabs' => 
		  array (
		    0 => 
		    array (
		      'op' => 'list',
		      'title' => '游戏模板列表',
		    ),
		  ),
		  'list' => 
		  array (
		    'data' => 'list',
		    'list' => 
		    array (
		      0 => 
		      array (
		        'type' => 'string',
		        'name' => 'id',
		        'title' => 'ID',
		      ),
		      1 => 
		      array (
		        'name' => 'name',
		        'type' => 'string',
		        'title' => '游戏名称',
		      ),
		  /*    2 => 
		      array (
		        'name' => 'type',
		        'type' => 'checkbox',
		        'title' => '活动类型',
		        'options' => 'type_xuanxiang',
		      ),
			  */
		      3 => 
		      array (
		        'name' => 'isrecommend',
		        'type' => 'radio',
		        'title' => '是否推荐',
		        'options' => 'isrecommend_xuanxiang',
		      ),
		      4 => 
		      array (
		        'name' => 'ispermission',
		        'type' => 'radio',
		        'title' => '是否拥有权限',
		        'options' => 'ispermission_xuanxiang',
		      ),
		      5 => 
		      array (
		        'type' => 'time',
		        'name' => 'createtime',
		        'title' => '创建时间',
		      ),
			  6 =>
			  array ('title' => '体验二维码','name' => 'url','type' => 'ewm', ),
		    ),
		  ),
		  
		  'search_one' => 
		  array (
		    'name' => 'name',
		  ),
		);
$return['type_xuanxiang']=array('1'=>'节日活动','2'=>'抽奖活动','3'=>'游戏营销','4'=>'助力活动','5'=>'投票活动','6'=>'签到活动');
$return['isrecommend_xuanxiang']=array('1'=>'是','2'=>'否');
$return['ispermission_xuanxiang']=array('1'=>'已拥有','2'=>'未拥有');

	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=$this->get('game',array('id'=>$id));

	}else{
		$item=array();
	}
	$return['item']=$item;
	$return['htmls']=array (
  'type' => 'edit',
  'post' => 'post',
  'edit_tab' => '添加活动模板',
  'edits' => 
  array (
    0 => 
    array (
      'title' => '添加活动模板',
      'list' => 
      array (
        0 => 
        array (
          'name' => 'name',
          'type' => 'string',
          'title' => '游戏名称',
        ),
        1 => 
        array (
          'name' => 'type',
          'type' => 'checkbox',
          'title' => '活动类型',
          'options' => 'type_xuanxiang',
        ),
        2 => 
        array (
          'name' => 'isrecommend',
          'type' => 'radio',
          'title' => '是否推荐',
          'options' => 'isrecommend_xuanxiang',
        ),
        3 => 
        array (
          'name' => 'ispermission',
          'type' => 'radio',
          'title' => '是否拥有权限',
          'options' => 'ispermission_xuanxiang',
        ),
        4 => 
        array (
          'name' => 'iamge',
          'type' => 'image',
          'title' => '活动图片',
        ),
      ),
    ),
  ),
);
$return['type_xuanxiang']=array('1'=>'节日活动','2'=>'抽奖活动','3'=>'游戏营销','4'=>'助力活动','5'=>'投票活动','6'=>'签到活动');
$return['isrecommend_xuanxiang']=array('1'=>'是','2'=>'否');
$return['ispermission_xuanxiang']=array('1'=>'已拥有','2'=>'未拥有');

	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$data=array_fields(array('name','type','isrecommend','ispermission','iamge',),$post);

	$id=intval($post['id']);
	if($id){
		$this->update('game',$data,array('id'=>$id));
		exi('修改市场活动成功！');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		$this->insert('game',$data);
		exi('新增市场活动成功！');
	}
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		$this->delete('game',array('id'=>$id));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->delete('game',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}